package com.ruoyi.service.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.service.utils.ServiceUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.service.domain.AbundantWater;
import com.ruoyi.service.service.IAbundantWaterService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 丰水期低谷时段弃水电量 信息操作处理
 * 
 * @author john
 * @date 2019-01-04
 */
@Controller
@RequestMapping("/service/abundantWater")
public class AbundantWaterController extends BaseController
{
    private String prefix = "service/abundantWater";
	
	@Autowired
	private IAbundantWaterService abundantWaterService;
	@Autowired
	private ServiceUtils utils;
	
	@GetMapping()
	public String abundantWater()
	{
	    return prefix + "/abundantWater";
	}
	
	/**
	 * 查询丰水期低谷时段弃水电量列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(AbundantWater abundantWater)
	{
		startPage();
        List<AbundantWater> list = abundantWaterService.selectAbundantWaterList(abundantWater);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出丰水期低谷时段弃水电量列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AbundantWater abundantWater)
    {
    	List<AbundantWater> list = abundantWaterService.selectAbundantWaterList(abundantWater);
        ExcelUtil<AbundantWater> util = new ExcelUtil<AbundantWater>(AbundantWater.class);
        return util.exportExcel(list, "abundantWater");
    }
	
	/**
	 * 新增丰水期低谷时段弃水电量
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存丰水期低谷时段弃水电量
	 */
	@Log(title = "丰水期低谷时段弃水电量", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(AbundantWater abundantWater)
	{		
		return toAjax(abundantWaterService.insertAbundantWater(abundantWater));
	}

	/**
	 * 修改丰水期低谷时段弃水电量
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		AbundantWater abundantWater = abundantWaterService.selectAbundantWaterById(id);
		mmap.put("month", utils.getMonth(abundantWater.getBasicsId()));
		mmap.put("abundantWater", abundantWater);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存丰水期低谷时段弃水电量
	 */
	@Log(title = "丰水期低谷时段弃水电量", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(AbundantWater abundantWater)
	{
		abundantWater.setFTotal(abundantWater.getFF().add(abundantWater.getFP()).add(abundantWater.getFG()));

		abundantWater.setUpAccount (abundantWater.getFactoryPrice().multiply(abundantWater.getFTotal()));
		abundantWater.setSPAccount(abundantWater.getSPPrice().multiply(abundantWater.getFTotal()));
		abundantWater.setJJAccount(abundantWater.getJJAverage().multiply(abundantWater.getFTotal()));
//		abundantWater.setEleAccount(abundantWater.getDHAverage().multiply(abundantWater.getFTotal()));

//		用户承担电费金额eleAccount = 电量上网电费upAccount + 输配收入金额sPAccount
		abundantWater.setEleAccount(abundantWater.getUpAccount().add(abundantWater.getSPAccount()));
//      到户均价dHAverage = 用户承担电费金额eleAccount / 丰水期电量小计fTotal
		BigDecimal TradeTotal = abundantWater.getFTotal();
		if (TradeTotal == BigDecimal.valueOf(0)) {
			abundantWater.setDHAverage(BigDecimal.ZERO);
		}else{
			abundantWater.setDHAverage(abundantWater.getEleAccount().divide(TradeTotal).setScale(6,BigDecimal.ROUND_HALF_UP));
		}

		int i = abundantWaterService.updateAbundantWater(abundantWater);
		utils.total(abundantWater.getPointId(),abundantWater.getBasicsId());
		return toAjax(i);
	}
	
	/**
	 * 删除丰水期低谷时段弃水电量
	 */
	@Log(title = "丰水期低谷时段弃水电量", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(abundantWaterService.deleteAbundantWaterByIds(ids));
	}
	
}
